package com.ac.cv.server.service;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.ac.cv.client.service.DataService;
import com.ac.cv.client.service.MenuItemDataClient;
import com.ac.cv.client.service.MenuMainItemDataClient;
import com.ac.cv.server.data.ClientToServer;
import com.ac.cv.server.data.MenuMainItemData;
import com.ac.cv.server.data.PMF;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class DataServiceImpl extends RemoteServiceServlet implements
		DataService {

	@SuppressWarnings("unchecked")
	@Override
	public List<MenuMainItemDataClient> getMenuMainItemData() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(MenuMainItemData.class);
		List<MenuMainItemDataClient> result = new ArrayList<MenuMainItemDataClient>();
		List<MenuMainItemData> listItem;
		try {
			listItem = (List<MenuMainItemData>) query.execute();
			for(MenuMainItemData item : listItem){
				result.add(ClientToServer.getMainFromServer(item));
			}
		} finally {
			pm.close();
		}
		
		
		return result;
	}

	@Override
	public void setMenuMainItemData(List<MenuMainItemDataClient> list) {
		PersistenceManager pm = PMF.get().getPersistenceManager();		
		try {
			for(MenuMainItemDataClient item : list){
				pm.makePersistent(ClientToServer.getMainFromClient(item));
			}
		} finally {
			pm.close();
		}
	}

	@Override
	public void setMenuItemData(MenuItemDataClient data) {
		PersistenceManager pm = PMF.get().getPersistenceManager();		
		try {
			pm.makePersistent(ClientToServer.getMainFromClient(data));
		} finally {
			pm.close();
		}
	}
}
